﻿/***********************************************************************
 *            Project: CoreCms.Net                                     *
 *                Web: https://CoreCms.Net                             *
 *        ProjectName: 核心内容管理系统                                *
 *             Author: 大灰灰                                          *
 *              Email: JianWeie@163.com                                *
 *           Versions: 1.0                                             *
 *         CreateTime: 2020-02-02 14:09:33
 *   ClassDescription: 
 ***********************************************************************/


using System;
using System.Collections.Generic;
using System.Linq;
using CoreCms.Net.Caching.Manual;

namespace CoreCms.Net.Caching.Redis
{
    public class RedisCacheManager : IManualCacheManager
    {

        /// <summary>
        /// 判断key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Exists(string key)
        {
            return RedisHelper.Exists(key);
        }


        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">缓存Value</param>
        /// <param name="expiresIn">缓存时间</param>
        /// <returns></returns>
        public bool Set(string key, object value, int expiresIn = 0)
        {
            if (value != null)
            {
                return expiresIn > 0 ? RedisHelper.Set(key, value, TimeSpan.FromMinutes(expiresIn)) : RedisHelper.Set(key, value);
            }
            return false;
        }

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public void Remove(string key)
        {
            RedisHelper.DelAsync(key);
        }

        /// <summary>
        /// 批量删除缓存
        /// </summary>
        /// <returns></returns>
        public void RemoveAll(IEnumerable<string> keys)
        {
            var enumerable = keys as string[] ?? keys.ToArray();
            string[] keyStrings = enumerable.ToArray();
            RedisHelper.DelAsync(keyStrings);
        }

        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public T Get<T>(string key)
        {
            return RedisHelper.Get<T>(key);
        }

        public object Get(string key)
        {
            return RedisHelper.Get<object>(key);
        }

        public IDictionary<string, object> GetAll(IEnumerable<string> keys)
        {
            if (keys == null)
                throw new ArgumentNullException(nameof(keys));
            var dict = new Dictionary<string, object>();

            keys.ToList().ForEach(item => dict.Add(item, RedisHelper.Get(item)));
            return dict;

        }

        public void RemoveCacheAll()
        {
            //查找所有分区节点中符合给定模式(pattern)的 key
            var cacheKeys = RedisHelper.Keys("*");
            RedisHelper.Del(cacheKeys);
        }

        public void RemoveCacheRegex(string pattern)
        {
            var cacheKeys = RedisHelper.Keys(pattern);
            RedisHelper.Del(cacheKeys);
        }

        public IList<string> SearchCacheRegex(string pattern)
        {
            return RedisHelper.Keys(pattern);
        }
    }
}
